package com.example.email;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

public class EmailService {
    @Resource
    private JavaMailSenderImpl mailSender;

    public void sendEasyMail(String from,String to,String subject,String text) throws MessagingException{
        //简单邮件
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(from);
        simpleMailMessage.setTo(to);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(text);
        mailSender.send(simpleMailMessage);
    }

    public void sendComplexMail(){
        //复杂邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
        try {
            messageHelper.setFrom("jiuyue@163.com");
            messageHelper.setTo("September@qq.com");
            messageHelper.setSubject("BugBugBug");
            messageHelper.setText("一杯茶，一根烟，一个Bug改一天！");
            messageHelper.addInline("bug.gif",  new File("xx/xx/bug.gif"));
            messageHelper.addAttachment("bug.docx",  new File("xx/xx/bug.docx"));
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }


}
